﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bzure.Common.Data;
using System.Reflection;
using System.Linq.Expressions;
using Bzure.Tools.Templates.Codes;

namespace Bzure.Tools.Templates
{
    public  class ORMGenerator : Generator
    {
        public string SectionName { get; private set; }
        public string ConnectionName { get; private set; }
        public ORMGenerator(string SectionName, string ConnectionName)
        {
            this.SectionName = SectionName;
            this.ConnectionName = ConnectionName;
        }
        Dictionary<Type, ORM> ORMs = new Dictionary<Type, ORM>();
        protected override IDictionary<string, Template> CreateTemplates()
        {
            Dictionary<string, Template> templates = new Dictionary<string, Template>();
            foreach (ORM orm in ORMs.Values)
            {
                templates.Add(string.Format("{0}-Repository.cs", orm.EntityType.Name), new EntityMapTemplate(orm.EntityType, this.SectionName, orm.TableName, orm.KeyField, orm.Columns));
            }
            templates.Add(string.Format("{0}-Provider.cs", this.SectionName), new EntityProviderTemplate(SectionName, this.ConnectionName, ORMs));
            return templates;
        }
        public ORMGenerator RegisterMapBuilder(IMapBuilder Builder)
        {
            foreach (ORM m in Builder.Build())
            {
                ORMs[m.EntityType] = m;
            }
            return this;
        }
      

    }
}
